function _getElementById(id) {
    return document.getElementById(id);
}
window.onload = function () {
    var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 1 });
    initSpread(spread);

    _getElementById("btnSetPrintInfo").onclick = function () {
        function setPrintInfoNumberValueItem(printInfo, key) {
            var value = parseInt(_getElementById(key).value);
            if (!isNaN(value)) {
                printInfo[key](value);
            }
        }

        var sheet = spread.getActiveSheet(),
            printInfo = sheet.printInfo();

        ["rowStart", "rowEnd", "columnStart", "columnEnd", "repeatRowStart", "repeatRowEnd", "repeatColumnStart", "repeatColumnEnd"].forEach(function (name) {
            setPrintInfoNumberValueItem(printInfo, name);
        });

        printInfo.showBorder(_getElementById("showBorder").checked);
        printInfo.showGridLine(_getElementById("showGridLine").checked);
        printInfo.blackAndWhite(_getElementById("blackAndWhite").checked);
        printInfo.showRowHeader(+_getElementById("showRowHeader").value);
        printInfo.showColumnHeader(+_getElementById("showColumnHeader").value);
        printInfo.headerLeft(_getElementById("headerLeft").value);
        printInfo.headerLeftImage(_getElementById("headerLeftImage").value);
        printInfo.headerCenter(_getElementById("headerCenter").value);
        printInfo.headerCenterImage(_getElementById("headerCenterImage").value);
        printInfo.headerRight(_getElementById("headerRight").value);
        printInfo.headerRightImage(_getElementById("headerRightImage").value);
        printInfo.footerLeft(_getElementById("footerLeft").value);
        printInfo.footerLeftImage(_getElementById("footerLeftImage").value);
        printInfo.footerCenter(_getElementById("footerCenter").value);
        printInfo.footerCenterImage(_getElementById("footerCenterImage").value);
        printInfo.footerRight(_getElementById("footerRight").value);
        printInfo.footerRightImage(_getElementById("footerRightImage").value);
    };
};

function adjustLayoutForPrint(sheet) {
    sheet.setRowHeight(0, 30);
    sheet.getRange(0, 0, 1, 5).hAlign(1).vAlign(1).font("bold 14px Times");
    sheet.setColumnWidth(0, 220);
    sheet.setColumnWidth(2, 120);
    sheet.setColumnWidth(3, 50);
    sheet.setColumnWidth(4, 180);

    sheet.getRange(1, 0, 200, 5).textIndent(1);
    sheet.getRange(-1, 3, -1, 1).hAlign(1).textIndent(0);

    sheet.addColumns(0, 1);
    sheet.setColumnWidth(0, 30);
    for (var r = 5; r <= 200; r += 5) {
        sheet
            .getCell(r, 0)
            .text(r + "")
            .font("normal 9px Times");
    }

    sheet.addRows(0, 1);
    sheet.setRowHeight(0, 10);

    sheet.getRange(1, 1, 202, 5).setBorder(new GC.Spread.Sheets.LineBorder("black", GC.Spread.Sheets.LineStyle.thin), { all: true });
}
function setPrintInfo(sheet) {
    var printInfo = sheet.printInfo();

    // custom printInfo for default output
    printInfo.showBorder(false);
    printInfo.showGridLine(false);
    printInfo.blackAndWhite(false);
    printInfo.columnEnd(5);
    printInfo.repeatRowStart(0);
    printInfo.repeatRowEnd(1);
    printInfo.showColumnHeader(GC.Spread.Sheets.Print.PrintVisibilityType.hide);
    printInfo.showRowHeader(GC.Spread.Sheets.Print.PrintVisibilityType.hide);
    printInfo.headerCenter("Olympic Athletes");
    printInfo.headerLeft("&G");
    printInfo.headerLeftImage("https://demo.grapecity.com.cn/spreadjs/SpreadJSTutorial/spread/source/images/olympic.jpg");
    printInfo.footerCenter("&P/&N");

    // sync with UI setting items
    _getElementById("rowStart").value = printInfo.rowStart();
    _getElementById("rowEnd").value = printInfo.rowEnd();
    _getElementById("columnStart").value = printInfo.columnStart();
    _getElementById("columnEnd").value = printInfo.columnEnd();
    _getElementById("repeatRowStart").value = printInfo.repeatRowStart();
    _getElementById("repeatRowEnd").value = printInfo.repeatRowEnd();
    _getElementById("repeatColumnStart").value = printInfo.repeatColumnStart();
    _getElementById("repeatColumnEnd").value = printInfo.repeatColumnEnd();
    _getElementById("showBorder").checked = printInfo.showBorder();
    _getElementById("showGridLine").checked = printInfo.showGridLine();
    _getElementById("blackAndWhite").checked = printInfo.blackAndWhite();
    _getElementById("showRowHeader").value = printInfo.showRowHeader();
    _getElementById("showColumnHeader").value = printInfo.showColumnHeader();
    _getElementById("headerLeft").value = printInfo.headerLeft();
    _getElementById("headerLeftImage").value = printInfo.headerLeftImage();
    _getElementById("headerCenter").value = printInfo.headerCenter();
    _getElementById("headerCenterImage").value = printInfo.headerCenterImage();
    _getElementById("headerRight").value = printInfo.headerRight();
    _getElementById("headerRightImage").value = printInfo.headerRightImage();
    _getElementById("footerLeft").value = printInfo.footerLeft();
    _getElementById("footerLeftImage").value = printInfo.footerLeftImage();
    _getElementById("footerCenter").value = printInfo.footerCenter();
    _getElementById("footerCenterImage").value = printInfo.footerCenterImage();
    _getElementById("footerRight").value = printInfo.footerRight();
    _getElementById("footerRightImage").value = printInfo.footerRightImage();
}

function initSpread(spread) {
    _getElementById("savePDF").onclick = function () {
        spread.savePDF(function (blob) {
            saveAs(blob, "download.pdf");
        }, console.log);
    };
    var sd = data;
    if (sd && sd.sheets) {
        if (!spread) {
            return;
        }
        spread.suspendPaint();

        spread.fromJSON(sd);

        var sheet = spread.sheets[0];
        sheet.suspendPaint();
        adjustLayoutForPrint(sheet);
        setPrintInfo(sheet);
        sheet.resumePaint();
        spread.resumePaint();
    }
}
